home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Gekikoh Dennoh Club 5
/
Gekikoh Dennoh Club Vol. 5 (Japan).7z
/
Gekikoh Dennoh Club Vol. 5 (Japan) (Track 01).bin
/
docs
/
rakup
/
match.vl
< prev
next >
Wrap
Lisp/Scheme
|
1998-10-03
|
3KB
|
130 lines
;
; match.vl : âpâ^ü[âôâ}âbâ`âôâO
;
; Copyright (C) 1998 by Makoto Hiroi
;
;
; üEò╧Éöé═ëpæσò╢ÄÜé┼Äné▄éΘâVâôâ{âï
; üEɼî≈é═ò╧Éöæ⌐ö¢é≡ò╘é╖
; üEÄ╕ösé═ fail é≡ò╘é╖
; üEû│û╝ò╧Éöé═û│é╡
;
;
; ò╧Éöé≡â`âFâbâNé╖éΘ
;
(defun variablep (pattern)
(and (symbolp pattern)
(upper-case-p (char pattern 0))))
;
; ò╧Éöæ⌐ö¢é╔Æ╟ë┴é╖éΘ
;
(defun add-binding (var value binding)
(cons (cons var value) binding))
;
; â}âbâ`âôâO : datum é╔ò╧Éöé═û│é╡
;
(defun match (pattern datum binding)
(cond ((variablep pattern)
(match-variable pattern datum binding))
((and (atom pattern) (atom datum))
(match-atoms pattern datum binding))
((and (consp pattern) (consp datum))
(match-pieces pattern datum binding))
(t 'fail)))
;
; ò╧Éöé╞é╠â}âbâ`âôâO
;
(defun match-variable (var datum binding)
(let ((value (assoc var binding)))
(if value
; Ælé≡Ägé┴é─éαéñêΩôxâ`âFâbâN
(match (cdr value) datum binding)
; ò╧Éöæ⌐ö¢é╔Æ╟ë┴é╖éΘ
(add-binding var datum binding))))
;
; âAâgâÇô»Ämé╠â}âbâ`âôâO
;
(defun match-atoms (pattern datum binding)
(if (equal pattern datum) binding 'fail))
;
; âèâXâgô»Ämé╠â}âbâ`âôâO
;
(defun match-pieces (pattern datum binding)
(let ((result (match (car pattern) (car datum) binding)))
(if (eq result 'fail)
'fail
(match (cdr pattern) (cdr datum) result))))
;
; âåâjâtâBâPü[âVâçâô : pattern, datum é╞éαé╔ò╧Éöé¬ôⁿé┴é─éóéΘ
;
(defun unify (pattern datum binding)
(cond ((variablep pattern)
(unify-variable pattern datum binding))
((variablep datum)
(unify-variable datum pattern binding))
((and (atom pattern) (atom datum))
(unify-atoms pattern datum binding))
((and (consp pattern) (consp datum))
(unify-pieces pattern datum binding))
(t 'fail)))
;
; âAâgâÇé╞é╠âåâjâtâBâPü[âVâçâô
;
(defun unify-atoms (pattern datum binding)
(if (equal pattern datum) binding 'fail))
;
; âèâXâgé╠âåâjâtâBâPü[âVâçâô
;
(defun unify-pieces (pattern datum binding)
(let ((result (unify (car pattern) (car datum) binding)))
(if (eq result 'fail)
'fail
(unify (cdr pattern) (cdr datum) result))))
;
; ò╧Éöé╞é╠âåâjâtâBâPü[âVâçâô
;
(defun unify-variable (pattern datum binding)
(let ((value (assoc pattern binding)))
(if (and value
; ((X . X) ... ) é┼û│î└âïü[âvé╔é╚éτé╚éóéµéñé╔é╖éΘé╜é▀
(not (eq pattern (cdr value))))
(unify (cdr value) datum binding)
(if (insidep pattern datum binding)
'fail
(add-binding pattern datum binding)))))
;
; datum é╠Æåé╔ var(ò╧Éö)é¬éáéΘé⌐
;
(defun insidep (var datum binding)
(if (eq var datum)
nil
(inside-sub-p var datum binding)))
(defun inside-sub-p (var datum binding)
(cond ((equal var datum) t)
((atom datum) nil)
((variablep datum)
(let ((value (assoc datum binding)))
(if value
(inside-sub-p var (cdr value) binding))))
(t ; list é╠ÅΩìç
(or (inside-sub-p var (car datum) binding)
(inside-sub-p var (cdr datum) binding)))))
; end of file